/**
 * @param {number[]} nums
 * @return {number[]}
 */
var productExceptSelf = function (nums) {
  let len = nums.length;
  let leftArr = [];
  let rightArr = [];

  leftArr[0] = 1;
  rightArr[len - 1] = 1;
  for (let i = 1; i < len; i++) {
    leftArr[i] = leftArr[i - 1] * nums[i - 1];
  }
  for (let i = len - 2; i >= 0; i--) {
    rightArr[i] = rightArr[i + 1] * nums[i + 1];
  }

  let res = [];
  for (let i = 0; i < len; i++) {
    res[i] = leftArr[i] * rightArr[i];
  }
  return res;
};

let nums1 = [1, 2, 3, 4];
let nums2 = [-1, 1, 0, -3, 3];

console.log(productExceptSelf(nums1));
